﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.Data;
using Common.Library;
using System.ComponentModel;
using Microsoft.Phone.Shell;
using Common.Utilities;
using OnlineVideos.View_Models;
using Microsoft.Phone.Controls;

namespace OnlineVideos
{
    public partial class DownloadedVideos : UserControl
    {
        OnlineVideos.View_Models.ShowDetail detailModel;
        List<ShowLinks> objSongList = null;
        private bool IsDataLoaded;
        public DownloadedVideos()
        {
            InitializeComponent();
            IsDataLoaded = false;
            detailModel = new OnlineVideos.View_Models.ShowDetail();
            objSongList = new List<ShowLinks>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                MyProgressBar1.IsIndeterminate = true;
                GetPageDataInBackground();
                IsDataLoaded = true;

            }
        }

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {
                                            a.Result = OnlineShow.GetShowLinksByType(AppSettings.ShowID, LinkType.Songs/*, false*/);
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {
                                            objSongList = (List<ShowLinks>)a.Result;
                                            if (objSongList.Count > 0)
                                            {
                                                lbxSongsList.ItemsSource = objSongList;
                                                tblkFavNoSongs.Visibility = Visibility.Collapsed;

                                                lbxSongsList.Loaded += new RoutedEventHandler(lbxSongsList_Loaded);

                                            }
                                            else
                                            {
                                                tblkFavNoSongs.Visibility = Visibility.Visible;
                                                tblkFavNoSongs.Text = "No " + AppResources.ShowDetailPageSongsPivotTitle as string + " available";
                                            }
                                            MyProgressBar1.IsIndeterminate = false;
                                        }
                                      );

            bwHelper.RunBackgroundWorkers();

        }

        void lbxSongsList_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.Keys.Contains("searchtitle"))
            {
                PageHelper.SetSelectedItemForegroundColor(this.lbxSongsList, AppState.searchtitle);

            }
            else
            {
                PageHelper.SetSelectedItemForegroundColor(this.lbxSongsList, "");
            }
        }

        private void lbxSongsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            ShowDetail detailModel = new ShowDetail();
            detailModel.DeleteDownloadedShowlinks(lbxSongsList, sender as MenuItem, LinkType.Songs);
            GetPageDataInBackground();
        }
    }
}
